********************************************************************************

/*Analysis script for 'Who Gives a Dime?'
Robert L. Mayo
Concordia College
Comments written 7/24/2019
Stata version: Stata IC 14.2

This script assumes the data is named "Raw Data.csv" and is located in the STATA working folder.
All output is saved to the working folder.  Log file and data file names are appended with a date/time stamp.
Under SETTINGS, you have the choice of whether to generate new graphs and a new output data file.
*/

********************************************************************************

clear all
set more off
ssc install outreg2, replace // for table formatting
ssc install coefplot, replace // for margins plots
*ssc install marginscontplot2, replace

// OPEN LOG FILE

	local c_date = c(current_date)
	local c_time = c(current_time)
	local c_time_date = "`c_date'"+"_" +"`c_time'"

	local time_string = subinstr("`c_time_date'", ":", "_", .)
	local time_string = subinstr("`time_string'", " ", "_", .)

	local logfilename = "Stata_log_file_"+"`time_string'"
	display "`logfilename'"

	*log using "`logfilename'", text

// SETTINGS

	// set working directory
	//cd "C:\Users\rmayo\Desktop\Stata working folder"  // I use this at work
	cd "C:\Users\basti\OneDrive\My Documents\Economics\Research\3 Research in Progress\Who Gives a Dime\Stata working folder"  // I use this at home

	local new_plots = 0  // set this to 1 if you want new plots or 0 if you want to supress them
	local new_datafile = 0  // set this to 1 if you want to save a new data file or 0 if you want to supress it

// IMPORT DATA

	import delimited "Raw Data.csv"

// DATA CLEANING

	// drop variables not needed for this analysis
	drop workerid hittypeid reward creationtime maxassignments assignmentid accepttime submittime worktimeinseconds ///
	answersurveycode v16 v17 ipaddress locationlatitude locationlongitude q5inthe2016presidentialelectionw ///
	q9whatstatedoyoulivein q7doyouconsideryourselfa q8asidefromweddingsandfuneralsho v27 v28
	
	//  replace qualtrics question names with clear variables names
	rename q3_1onascaleof0to100howwouldyoud liberal
	rename q8_1onascaleof0to100howwouldyoud religious
	
	// recode missing values
	replace liberal = "" if liberal == "#N/A"
	replace religious = "" if religious == "#N/A"
	
	// convert from string to numeric
	destring liberal, replace
	destring religious, replace
	
	// convert from byte to float
	recast float liberal
	recast float religious

	//this makes political left appear on the left side of plots
	gen conservative = 100 - liberal 
	label variable conservative "conservative"
	drop liberal

	// create dummy variable for gender
	gen female = 1
	replace female = 0 if gender == "Male"

	// this makes income categories sort in the correct order
	replace income = "$0 - $19,999" if income == "Under $20,000"
	replace income = "$100,000 or more" if income == "Over $100,000"

	// Impute values at means for age and income categories.  _c means continuous
	gen age_c = .
	replace age_c = 23.5 if age == "18-29"
	replace age_c = 34.5 if age == "30-39"
	replace age_c = 44.5 if age == "40-49"
	replace age_c = 54.5 if age == "50-59"
	replace age_c = 64.5 if age == "60-69"
	replace age_c = 74.5 if age == "70 or over"

	gen income_c = .
	replace income_c = 10 if income == "$0 - $19,999"
	replace income_c = 30 if income == "$20,000 - $39,999"
	replace income_c = 50 if income == "$40,000 - $59,999"
	replace income_c = 70 if income == "$60,000 - $79,999"
	replace income_c = 90 if income == "$80,000 - $99,999"
	replace income_c = 110 if income == "$100,000 or more"

	// create quintile categories
	gen religious_quintile = .
	replace religious_quintile = 1 if religious >= 0 & religious < 20
	replace religious_quintile = 2 if religious >= 20 & religious < 40
	replace religious_quintile = 3 if religious >= 40 & religious < 60
	replace religious_quintile = 4 if religious >= 60 & religious < 80
	replace religious_quintile = 5 if religious >= 80 & religious <= 100

	gen conservative_quintile = .
	replace conservative_quintile = 1 if conservative >= 0 & conservative < 20
	replace conservative_quintile = 2 if conservative >= 20 & conservative < 40
	replace conservative_quintile = 3 if conservative >= 40 & conservative < 60
	replace conservative_quintile = 4 if conservative >= 60 & conservative < 80
	replace conservative_quintile = 5 if conservative >= 80 & conservative <= 100

	// create categories that divide demo groups in two
	gen age_over_40 = 0
	replace age_over_40 = 1 if age_c > 40
	gen income_over_60 = 0
	replace income_over_60 = 1 if income_c > 60
	gen conservative_over_50 = .
	replace conservative_over_50 = 0 if conservative >= 0 & conservative <= 50
	replace conservative_over_50 = 1 if conservative > 50 & conservative <= 100
	gen religious_over_50 = .
	replace religious_over_50 = 0 if religious >= 0 & religious <= 50
	replace religious_over_50 = 1 if religious > 50 & religious <= 100
	
	// create dummy for sent a positive amount
	gen did_send = 1
	replace did_send = 0 if amount == 0

	// this fixes one observation where the multiplier was coded as 0 instead of 0.01 due to rounding
	replace multiplier = 0.01 if multiplier == 0

	// generate variables for regressions
	gen price = 1/multiplier
	gen ln_price = ln(price)
	gen adj_amount = amount + 0.01
	gen ln_adj_amount = ln(adj_amount)
	gen ln_mu = ln(multiplier)

	// count and drop cases where subjects entered invalid responses or refused to answer
	count if age == "0" | age == ""
	count if age == "Prefer not to state"
	count if gender == "Other / Prefer not to state"
	count if income == "Prefer not to state"
	count if  age == "0" | age == "" | age == "Prefer not to state" | gender == "Other / Prefer not to state" | income == "Prefer not to state"
	
	drop if age == "0" | age == ""
	drop if age == "Prefer not to state"
	drop if gender == "Other / Prefer not to state"
	drop if income == "Prefer not to state"

	// create dummy variable for subjects who completed the follow-up demographic survey
	gen survey = 0
	replace survey = 1 if conservative != .
	
	// IS THERE A RELATIONSHIP BETWEEN AMOUNT SENT AND COMPLETING THE SURVEY?
	ttest amount, by(survey)
	
	// count and drop cases where subject completed the main experiment but not the demographic survey
	drop if survey == 0
	drop survey

// SUMMARY STATISTICS

	// Sample demographics
	sum
	tabulate gender
	tabulate age
	tabulate income
	sum age_c, detail
	sum income_c, detail
	sum conservative, detail
	sum religious, detail
	count if amount == 0
	count if amount == 1
	count if amount > 0 & amount < 1
	sum amount if amount > 0

	// correlations between demographic variables
	pwcorr age_c income_c religious conservative, sig

	// amount sent in full sample
	sum multiplier, detail
	sum amount, detail
	sum amount if amount == 0
	sum amount if amount == 1
	sum amount if amount > 0 & amount < 1
	sum amount if amount > 0, detail

	// amount sent by demographic groups
	by gender, sort : summarize amount
	by gender, sort : summarize amount if amount == 0
	by gender, sort : summarize amount if amount != 0
	by age_over_40, sort : summarize amount
	by age_over_40, sort : summarize amount if amount == 0
	by age_over_40, sort : summarize amount if amount != 0
	by income_over_60, sort : summarize amount
	by income_over_60, sort : summarize amount if amount == 0
	by income_over_60, sort : summarize amount if amount != 0
	by conservative_over_50, sort : summarize amount
	by conservative_over_50, sort : summarize amount if amount == 0
	by conservative_over_50, sort : summarize amount if amount != 0
	by religious_over_50, sort : summarize amount
	by religious_over_50, sort : summarize amount if amount == 0
	by religious_over_50, sort : summarize amount if amount != 0

// DOES AMOUNT SENT VARY BY DEMOGRAPHIC GROUP?

	ttest amount, by(female)
	spearman amount age_c, stats(rho p)
	spearman amount income_c, stats(rho p)
	spearman amount conservative, stats(rho p)
	spearman amount religious, stats(rho p)
	
	// these are to show robustness
	reg amount female income_c age_c conservative religious
	outreg2 using reg_table_ols.doc, replace

// DOES TOTAL PRICE ELASTICITY VARY BY DEMOGRAPHIC GROUP? 

	tobit ln_adj_amount c.ln_price##i.female c.ln_price##c.age_c c.ln_price##c.income_c c.ln_price##c.conservative c.ln_price##c.religious, ll ul

	
	
	*rebustness check of usinf multiplier instead of price
	*gen ln_multiplier = ln(multiplier)
	*tobit ln_adj_amount c.ln_multiplier##i.female c.ln_multiplier##c.age_c c.ln_multiplier##c.income_c c.ln_multiplier##c.conservative c.ln_multiplier##c.religious, ll ul
	
	
	/*
	// DEMAND PLOT TESTS

	// GENERATE DEMAND PREDICTIONS

	*reg amount c.price##i.female c.price##c.age_c c.price##c.income_c c.price##c.conservative c.price##c.religious
	*reg amount c.multiplier##i.female c.multiplier##c.age_c c.multiplier##c.income_c c.multiplier##c.conservative c.multiplier##c.religious
	
	tobit amount c.multiplier##i.female c.multiplier##c.age_c c.multiplier##c.income_c c.multiplier##c.conservative c.multiplier##c.religious, ll ul
	margins, at(c.multiplier=(0.0(0.25)2.0))
	marginsplot, recast(line) recastci(rarea)
	
	
	
	*tobit amount c.price##i.female c.price##c.age_c c.price##c.income_c c.price##c.conservative c.price##c.religious, ll ul


	*THIS WAY USES ALL OTHER VARIABLES AT THEIR MEANS
	// set all demographic vales to their means
	egen mean_female = mean (female)
	egen mean_age = mean (age_c)
	egen mean_income = mean (income_c)
	egen mean_conservative = mean (conservative)
	egen mean_religious = mean (religious)
	
	sum mean_female mean_age mean_income mean_conservative mean_religious
	
	
	replace female = mean_female
	replace age_c = mean_age
	replace income_c = mean_income
	replace conservative = mean_conservative
	replace religious = mean_religious


	// create predicted amounts sent at representative demographic values
	// with all other demographic variables set at their means
	
	// predictions for full sample
	predict sent_all
	
	// predictions for female 0/1
	replace female = 0
	predict sent_female
	replace female = 1
	predict sent_male
	replace female = mean_female
	
	// predictions for age 25/65
	replace age_c = 25
	predict sent_age25
	replace age_c = 65
	predict sent_age65
	replace age_c = mean_age
	
	// predictions for income 20k/100k
	replace income_c = 20
	predict sent_income20
	replace income_c = 100
	predict sent_income100
	replace income_c = mean_income
	
	// predictions for conservative 0/100
	replace conservative = 0
	predict sent_conservative0
	replace conservative = 100
	predict sent_conservative100
	replace conservative = mean_conservative
	
	// predictions for religious 0 / 100
	replace religious = 0
	predict sent_religious0
	replace religious = 100
	predict sent_religious100
	replace religious = mean_religious
	

	twoway line multiplier sent_all, ysc(reverse) title(all) name(all)
	twoway line multiplier sent_female, ysc(reverse) || line multiplier sent_male, ysc(reverse) xsc(range(0 0.5)) title(sex) name(sex)
	twoway line multiplier sent_age25, ysc(reverse) || line multiplier sent_age65, ysc(reverse)  xsc(range(0 0.5)) title(age) name(age)
	twoway line multiplier sent_income20, ysc(reverse) || line multiplier sent_income100, ysc(reverse)  xsc(range(0 0.5)) title(income) name(income)
	twoway line multiplier sent_conservative0, ysc(reverse) || line multiplier sent_conservative100, ysc(reverse) xsc(range(0 0.5)) title(politics) name(politics)
	twoway line multiplier sent_religious0, ysc(reverse) || line multiplier sent_religious100, ysc(reverse) xsc(range(0 0.5)) title(religion) name(religion)

	graph combine all sex age income politics religion, name(demand_combined)
	graph save demand_combined, replace
	
	/*
	twoway scatter multiplier sent_all, ysc(reverse) title(all) name(all)
	twoway scatter multiplier sent_female, ysc(reverse) || scatter multiplier sent_male, ysc(reverse) xsc(range(0 0.5)) title(sex) name(sex)
	twoway scatter multiplier sent_age25, ysc(reverse) || scatter multiplier sent_age65, ysc(reverse)  xsc(range(0 0.5)) title(age) name(age)
	twoway scatter multiplier sent_income20, ysc(reverse) || scatter multiplier sent_income100, ysc(reverse)  xsc(range(0 0.5)) title(income) name(income)
	twoway scatter multiplier sent_conservative0, ysc(reverse) || scatter multiplier sent_conservative100, ysc(reverse) xsc(range(0 0.5)) title(politics) name(politics)
	twoway scatter multiplier sent_religious0, ysc(reverse) || scatter multiplier sent_religious100, ysc(reverse) xsc(range(0 0.5)) title(religion) name(religion)

	graph combine all sex age income politics religion
	*/
	
	graph save demand_rel_0, replace
	
	*/
	
	
	outreg2 using reg_table_tobit.doc, replace
	
// ESTIMATE TOTAL PRICE ELASTICITIES
	
	// create matrix t_elasticities to store total elasticities and 95% confidence intervals and standard errors
	matrix t_elasticities = J(11,4,.)
	matrix colnames t_elasticities = estimate lower_CI upper_CI se
	matrix rownames t_elasticities = all male female age_25 age_65 income_$20k income_$100k conservative_0 conservative_100 religious_0 religious_100
	matrix list t_elasticities

	// full sample
	tobit ln_adj_amount c.ln_price##i.female c.ln_price##c.age_c c.ln_price##c.income_c c.ln_price##c.conservative c.ln_price##c.religious, ll ul
	
	margins, dydx(ln_price) atmeans post
	matrix temp = r(table)
	matrix list temp
	matrix t_elasticities[1,1] = temp[1,1]
	matrix t_elasticities[1,4] = temp[2,1]
	matrix t_elasticities[1,2] = temp[5,1]
	matrix t_elasticities[1,3] = temp[6,1]
	
	matrix list t_elasticities

	// male
	tobit ln_adj_amount c.ln_price##i.female c.ln_price##c.age_c c.ln_price##c.income_c c.ln_price##c.conservative c.ln_price##c.religious, ll ul
	margins, dydx(ln_price) at(female=0) atmeans post
	matrix temp = r(table)
	matrix t_elasticities[2,1] = temp[1,1]
	matrix t_elasticities[2,4] = temp[2,1]
	matrix t_elasticities[2,2] = temp[5,1]
	matrix t_elasticities[2,3] = temp[6,1]

	// female
	tobit ln_adj_amount c.ln_price##i.female c.ln_price##c.age_c c.ln_price##c.income_c c.ln_price##c.conservative c.ln_price##c.religious, ll ul
	margins, dydx(ln_price) at(female=1) atmeans post
	matrix temp = r(table)
	matrix t_elasticities[3,1] = temp[1,1]
	matrix t_elasticities[3,4] = temp[2,1]
	matrix t_elasticities[3,2] = temp[5,1]
	matrix t_elasticities[3,3] = temp[6,1]

	// age 25
	tobit ln_adj_amount c.ln_price##i.female c.ln_price##c.age_c c.ln_price##c.income_c c.ln_price##c.conservative c.ln_price##c.religious, ll ul
	margins, dydx(ln_price) at(age_c=25) atmeans post
	matrix temp = r(table)
	matrix t_elasticities[4,1] = temp[1,1]
	matrix t_elasticities[4,4] = temp[2,1]
	matrix t_elasticities[4,2] = temp[5,1]
	matrix t_elasticities[4,3] = temp[6,1]

	// age 65
	tobit ln_adj_amount c.ln_price##i.female c.ln_price##c.age_c c.ln_price##c.income_c c.ln_price##c.conservative c.ln_price##c.religious, ll ul
	margins, dydx(ln_price) at(age_c=65) atmeans post
	matrix temp = r(table)
	matrix t_elasticities[5,1] = temp[1,1]
	matrix t_elasticities[5,4] = temp[2,1]
	matrix t_elasticities[5,2] = temp[5,1]
	matrix t_elasticities[5,3] = temp[6,1]

	// income $20k
	tobit ln_adj_amount c.ln_price##i.female c.ln_price##c.age_c c.ln_price##c.income_c c.ln_price##c.conservative c.ln_price##c.religious, ll ul
	margins, dydx(ln_price) at(income_c=20) atmeans post
	matrix temp = r(table)
	matrix t_elasticities[6,1] = temp[1,1]
	matrix t_elasticities[6,4] = temp[2,1]
	matrix t_elasticities[6,2] = temp[5,1]
	matrix t_elasticities[6,3] = temp[6,1]

	// income $100k
	tobit ln_adj_amount c.ln_price##i.female c.ln_price##c.age_c c.ln_price##c.income_c c.ln_price##c.conservative c.ln_price##c.religious, ll ul
	margins, dydx(ln_price) at(income_c=100) atmeans post
	matrix temp = r(table)
	matrix t_elasticities[7,1] = temp[1,1]
	matrix t_elasticities[7,4] = temp[2,1]
	matrix t_elasticities[7,2] = temp[5,1]
	matrix t_elasticities[7,3] = temp[6,1]

	// conservative = 0
	tobit ln_adj_amount c.ln_price##i.female c.ln_price##c.age_c c.ln_price##c.income_c c.ln_price##c.conservative c.ln_price##c.religious, ll ul
	margins, dydx(ln_price) at(conservative=0) atmeans post
	matrix temp = r(table)
	matrix t_elasticities[8,1] = temp[1,1]
	matrix t_elasticities[8,4] = temp[2,1]
	matrix t_elasticities[8,2] = temp[5,1]
	matrix t_elasticities[8,3] = temp[6,1]

	// conservative = 100
	tobit ln_adj_amount c.ln_price##i.female c.ln_price##c.age_c c.ln_price##c.income_c c.ln_price##c.conservative c.ln_price##c.religious, ll ul
	margins, dydx(ln_price) at(conservative=100) atmeans post
	matrix temp = r(table)
	matrix t_elasticities[9,1] = temp[1,1]
	matrix t_elasticities[9,4] = temp[2,1]
	matrix t_elasticities[9,2] = temp[5,1]
	matrix t_elasticities[9,3] = temp[6,1]

	// religious = 0
	tobit ln_adj_amount c.ln_price##i.female c.ln_price##c.age_c c.ln_price##c.income_c c.ln_price##c.conservative c.ln_price##c.religious, ll ul
	margins, dydx(ln_price) at(religious=0) atmeans post
	matrix temp = r(table)
	matrix t_elasticities[10,1] = temp[1,1]
	matrix t_elasticities[10,4] = temp[2,1]
	matrix t_elasticities[10,2] = temp[5,1]
	matrix t_elasticities[10,3] = temp[6,1]

	// religious = 100
	tobit ln_adj_amount c.ln_price##i.female c.ln_price##c.age_c c.ln_price##c.income_c c.ln_price##c.conservative c.ln_price##c.religious, ll ul
	margins, dydx(ln_price) at(religious=100) atmeans post
	matrix temp = r(table)
	matrix t_elasticities[11,1] = temp[1,1]
	matrix t_elasticities[11,4] = temp[2,1]
	matrix t_elasticities[11,2] = temp[5,1]
	matrix t_elasticities[11,3] = temp[6,1]
	
	matrix list t_elasticities

	// to show robustness
	reg ln_adj_amount c.ln_price##i.female c.ln_price##c.age_c c.ln_price##c.income_c c.ln_price##c.conservative c.ln_price##c.religious, robust
	
// DOES EXTENSIVE PRICE ELASTICITY VARY BY DEMOGRAPHIC GROUP? 

	probit did_send c.ln_price##i.female c.ln_price##c.age_c c.ln_price##c.income_c c.ln_price##c.conservative c.ln_price##c.religious
	outreg2 using reg_table_probit.doc, replace
	
// ESTIMATE TOTAL PRICE ELASTICITIES
	
	// create matrix e_elasticities to store extensive elasticities and 95% confidence intervals and standard errors
	matrix e_elasticities = J(11,4,.)
	matrix colnames e_elasticities = estimate lower_CI upper_CI se
	matrix rownames e_elasticities = all male female age_25 age_65 income_$20k income_$100k conservative_0 conservative_100 religious_0 religious_100

	// full sample
	probit did_send c.ln_price##i.female c.ln_price##c.age_c c.ln_price##c.income_c c.ln_price##c.conservative c.ln_price##c.religious
	margins, dydx(ln_price) atmeans post
	matrix temp = r(table)
	matrix e_elasticities[1,1] = temp[1,1]
	matrix e_elasticities[1,4] = temp[2,1]
	matrix e_elasticities[1,2] = temp[5,1]
	matrix e_elasticities[1,3] = temp[6,1]

	// male
	probit did_send c.ln_price##i.female c.ln_price##c.age_c c.ln_price##c.income_c c.ln_price##c.conservative c.ln_price##c.religious
	margins, dydx(ln_price) at(female=0) atmeans post
	matrix temp = r(table)
	matrix e_elasticities[2,1] = temp[1,1]
	matrix e_elasticities[2,4] = temp[2,1]
	matrix e_elasticities[2,2] = temp[5,1]
	matrix e_elasticities[2,3] = temp[6,1]

	// female
	probit did_send c.ln_price##i.female c.ln_price##c.age_c c.ln_price##c.income_c c.ln_price##c.conservative c.ln_price##c.religious
	margins, dydx(ln_price) at(female=1) atmeans post
	matrix temp = r(table)
	matrix e_elasticities[3,1] = temp[1,1]
	matrix e_elasticities[3,4] = temp[2,1]
	matrix e_elasticities[3,2] = temp[5,1]
	matrix e_elasticities[3,3] = temp[6,1]

	// age 25
	probit did_send c.ln_price##i.female c.ln_price##c.age_c c.ln_price##c.income_c c.ln_price##c.conservative c.ln_price##c.religious
	margins, dydx(ln_price) at(age_c=25) atmeans post
	matrix temp = r(table)
	matrix e_elasticities[4,1] = temp[1,1]
	matrix e_elasticities[4,4] = temp[2,1]
	matrix e_elasticities[4,2] = temp[5,1]
	matrix e_elasticities[4,3] = temp[6,1]

	// age 65
	probit did_send c.ln_price##i.female c.ln_price##c.age_c c.ln_price##c.income_c c.ln_price##c.conservative c.ln_price##c.religious
	margins, dydx(ln_price) at(age_c=65) atmeans post
	matrix temp = r(table)
	matrix e_elasticities[5,1] = temp[1,1]
	matrix e_elasticities[5,4] = temp[2,1]
	matrix e_elasticities[5,2] = temp[5,1]
	matrix e_elasticities[5,3] = temp[6,1]

	// income $20k
	probit did_send c.ln_price##i.female c.ln_price##c.age_c c.ln_price##c.income_c c.ln_price##c.conservative c.ln_price##c.religious
	margins, dydx(ln_price) at(income_c=20) atmeans post
	matrix temp = r(table)
	matrix e_elasticities[6,1] = temp[1,1]
	matrix e_elasticities[6,4] = temp[2,1]
	matrix e_elasticities[6,2] = temp[5,1]
	matrix e_elasticities[6,3] = temp[6,1]

	// income $100k
	probit did_send c.ln_price##i.female c.ln_price##c.age_c c.ln_price##c.income_c c.ln_price##c.conservative c.ln_price##c.religious
	margins, dydx(ln_price) at(income_c=100) atmeans post
	matrix temp = r(table)
	matrix e_elasticities[7,1] = temp[1,1]
	matrix e_elasticities[7,4] = temp[2,1]
	matrix e_elasticities[7,2] = temp[5,1]
	matrix e_elasticities[7,3] = temp[6,1]

	// conservative = 0
	probit did_send c.ln_price##i.female c.ln_price##c.age_c c.ln_price##c.income_c c.ln_price##c.conservative c.ln_price##c.religious
	margins, dydx(ln_price) at(conservative=0) atmeans post
	matrix temp = r(table)
	matrix e_elasticities[8,1] = temp[1,1]
	matrix e_elasticities[8,4] = temp[2,1]
	matrix e_elasticities[8,2] = temp[5,1]
	matrix e_elasticities[8,3] = temp[6,1]

	// conservative = 100
	probit did_send c.ln_price##i.female c.ln_price##c.age_c c.ln_price##c.income_c c.ln_price##c.conservative c.ln_price##c.religious
	margins, dydx(ln_price) at(conservative=100) atmeans post
	matrix temp = r(table)
	matrix e_elasticities[9,1] = temp[1,1]
	matrix e_elasticities[9,4] = temp[2,1]
	matrix e_elasticities[9,2] = temp[5,1]
	matrix e_elasticities[9,3] = temp[6,1]

	// religious = 0
	probit did_send c.ln_price##i.female c.ln_price##c.age_c c.ln_price##c.income_c c.ln_price##c.conservative c.ln_price##c.religious
	margins, dydx(ln_price) at(religious=0) atmeans post
	matrix temp = r(table)
	matrix e_elasticities[10,1] = temp[1,1]
	matrix e_elasticities[10,4] = temp[2,1]
	matrix e_elasticities[10,2] = temp[5,1]
	matrix e_elasticities[10,3] = temp[6,1]

	// religious = 100
	probit did_send c.ln_price##i.female c.ln_price##c.age_c c.ln_price##c.income_c c.ln_price##c.conservative c.ln_price##c.religious
	margins, dydx(ln_price) at(religious=100) atmeans post
	matrix temp = r(table)
	matrix e_elasticities[11,1] = temp[1,1]
	matrix e_elasticities[11,4] = temp[2,1]
	matrix e_elasticities[11,2] = temp[5,1]
	matrix e_elasticities[11,3] = temp[6,1]

	matrix list t_elasticities
	matrix list e_elasticities


// PLOTS

	if `new_plots' {

		// mean sent by gender
		preserve
		collapse amount (sem) sterr=amount, by(gender)
		gen ul = amount + 1.96*sterr
		gen ll = amount - 1.96*sterr
		encode gender, gen(category)
		twoway (connected amount category, sort) (rcap ul ll category, sort), title(Mean amount sent by gender) saving(mean_sent_by_gender,replace)
		graph save graph_sent_by_gender, replace
		restore

		// mean sent by age
		preserve
		collapse amount (sem) sterr=amount, by(age)
		gen ul = amount + 1.96*sterr
		gen ll = amount - 1.96*sterr
		encode age, gen(category)
		twoway (connected amount category, sort) (rcap ul ll category, sort), title(Mean amount sent by age) saving(mean_sent_by_age,replace)
		graph save graph_sent_by_age, replace
		restore

		// mean sent by income
		preserve
		collapse amount (sem) sterr=amount, by(income)
		gen ul = amount + 1.96*sterr
		gen ll = amount - 1.96*sterr
		encode income, gen(category)
		twoway (connected amount category, sort) (rcap ul ll category, sort), title(Mean amount sent by income) saving(mean_sent_by_income,replace)
		graph save graph_sent_by_income, replace
		restore

		// mean sent by conservative
		preserve
		collapse amount (sem) sterr=amount, by(conservative_quintile)
		gen ul = amount + 1.96*sterr
		gen ll = amount - 1.96*sterr
		twoway (connected amount conservative_quintile, sort) (rcap ul ll conservative_quintile, sort), title(Mean amount sent by conservative quintile) saving(mean_sent_by_conservative_quintile,replace)
		graph save graph_sent_by_conservative, replace
		restore

		// mean sent by religiosity
		preserve
		collapse amount (sem) sterr=amount, by(religious_quintile)
		gen ul = amount + 1.96*sterr
		gen ll = amount - 1.96*sterr
		twoway (connected amount religious_quintile, sort) (rcap ul ll religious_quintile, sort), title(Mean amount sent by religiosity quintile) saving(mean_sent_by_religious_quintile,replace)
		graph save graph_sent_by_religious, replace
		restore
		
		// demographic distribution of sample complete cases
		preserve
		graph bar (count), over(gender) name(gender_counts)
		graph save graph_count_by_gender, replace
		graph bar (count), over(age) name(age_counts)
		graph save graph_count_by_age, replace
		graph bar (count), over(income) name(income_counts)
		graph save graph_count_by_income, replace
		graph bar (count) if conservative_quintile != 0, over (conservative_quintile) name(conservative_counts)
		graph save graph_count_by_conservative, replace
		graph bar (count) if religious_quintile != 0, over (religious_quintile) name(religious_counts)
		graph save graph_count_by_religious, replace
		graph combine gender_counts age_counts income_counts conservative_counts religious_counts
		graph save graph_count_all, replace
		restore
		
		histogram conservative, width(10) percent title(Conservative) saving(conservative_histogram,replace)
		histogram religious, width(10) percent title(Religious) saving(religious_histogram,replace)
		
		// elasticity plots		
		coefplot matrix(t_elasticities[,1]), ci((t_elasticities[,2] t_elasticities[,3])) xline(0) title(placeholder title) xtitle(Total price elasticity) ytitle(placeholder) ///
			coeflabels(all = "Full sample" male = "Male" female = "Female" age_25 = "Age 25" age_65 = "Age 65" income_$20k = "Income $20,000" income_$100k = "Income $100,000" ///
			conservative_0 = "Conservative = 0" conservative_100 = "Conservative = 100" religious_0 = "Religious = 0" religious_100 = "Religious = 100")
		graph save elasticities_total, replace
		
		coefplot matrix(e_elasticities[,1]), ci((e_elasticities[,2] e_elasticities[,3])) xline(0) title(placeholder title) xtitle(Extensive price elasticity) ytitle(placeholder) ///
			coeflabels(all = "Full sample" male = "Male" female = "Female" age_25 = "Age 25" age_65 = "Age 65" ///
			income_$20k = "Income $20,000" income_$100k = "Income $100,000" conservative_0 = "Conservative = 0" conservative_100 = "Conservative = 100" religious_0 = "Religious = 0" religious_100 = "Religious = 100")
		graph save elasticities_extensive, replace
	}

// DATA EXPORT

	if `new_datafile' {
		local c_date = c(current_date)
		local c_time = c(current_time)

		local c_time_date = "`c_date'"+"_" +"`c_time'"
		display "`c_time_date'"

		local time_string = subinstr("`c_time_date'", ":", "_", .)
		local time_string = subinstr("`time_string'", " ", "_", .)
		display "`time_string'"

		local filename = "Stata_data_export_"+"`time_string'"
		display "`filename'"

		export excel using "`filename'", firstrow(variables)
	}

// CLOSE LOG FILE
	log close



	
	
	
	
	




// END OF EXTRA LINES FOR READABILITY



